package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Drug;
import com.hp.service.DrugService;
import com.hp.util.JsonData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/drug")
public class DrugController {
    @Resource
    private DrugService drugService;

    /**
     * 药物查询
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Object findAll(Drug drug, @RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "limit",defaultValue = "10") Integer limit){
        System.out.println(drug);
        PageHelper.startPage(page,limit);
        List<Drug> list = drugService.findAll(drug);
        System.out.println(list);
        PageInfo pageInfo = new PageInfo<>(list,limit);
        return JsonData.buildSuccess(pageInfo);
    }
    /**
     * 删除
     */
    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer id){
        int i=drugService.del(id);
        return JsonData.buildSuccess(i>0?"删除成功!":"删除失败!");

    }

    /**
     * 根据id查询
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Integer id) {
        Drug hisDrug = drugService.findById(id);
        System.out.println(hisDrug);
        return JsonData.buildSuccess(hisDrug);
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Drug obj){
        System.out.println(obj);
        int i = drugService.update(obj);
        return JsonData.buildSuccess(i>0?"修改成功！":"修改失败！");
    }
    /**
     * 添加库存数量
     */
    @RequestMapping("/upd")
    @ResponseBody
    public Object upd(Drug obj){
        System.out.println(obj);
        int i = drugService.upd(obj);
        return JsonData.buildSuccess(i>0?"入库成功！":"入库失败！");
    }
    /**
     * 添加
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Drug obj){
        int i=drugService.add(obj);
        System.out.println(i);
        return JsonData.buildSuccess(i>0?"添加成功！":"添加失败！");
    }
}
